package com.jsonparse;

import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MainActivity extends AppCompatActivity {
    private final String TAG = "MainActivity";
    private ArrayAdapter<String> provinceAdapter;
    private ArrayAdapter<String> cityAdapter;
    private ArrayAdapter<String> areaAdapter;
    private Spinner provinceSpinner;
    private Spinner citySpinner;
    private Spinner areaSpinner;
    private String[] provinceNames;
    private String[] cityNames;
    private String[] areaNames;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bindView();
        setSpinner();
    }

    private void setSpinner() {
        try {
            //获取数据并存入StringBuffer
            InputStream inputStream = getResources().getAssets().open("data.txt");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            final StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }

            //获取provinceJsonArray和初始化provinceNames数组
            final JSONArray provinceJsonArray = new JSONArray(stringBuffer.toString());
            provinceNames = new String[provinceJsonArray.length()];

            //for循环获取provinceName并存入provinceNames数组
            for (int i = 0; i < provinceJsonArray.length(); i++) {
                JSONObject provinceObejct = provinceJsonArray.getJSONObject(i);
                String provinceName = provinceObejct.getString("name");
                provinceNames[i] = provinceName;
            }

            //设置provinceSpinner的Adapter
            provinceAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, provinceNames);
            provinceSpinner.setAdapter(provinceAdapter);

            //设置provinceSpinner的选择事件
            provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    try {
                        //获取cityJsonArray和初始化cityNames数组
                        final JSONArray cityJsonArray = provinceJsonArray.getJSONObject(position).getJSONArray("city");
                        cityNames = new String[cityJsonArray.length()];

                        //for循环获取cityName存入cityNames数组
                        for (int j = 0; j < cityJsonArray.length(); j++) {
                            final JSONObject cityJsonObject = cityJsonArray.getJSONObject(j);
                            String cityName = cityJsonObject.getString("name");
                            cityNames[j] = cityName;

                            //设置citySpinner的Adapter
                            cityAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, cityNames);
                            citySpinner.setAdapter(cityAdapter);

                            //设置citySpinner的选择事件
                            citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                @Override
                                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                    try {
                                        //获取areaJsonArray和初始化areaNames数组
                                        JSONArray areaJsonArray = cityJsonArray.getJSONObject(position).getJSONArray("area");
                                        areaNames = new String[areaJsonArray.length()];

                                        //for循环获取areaName存入areaNames数组
                                        for (int k = 0; k < areaJsonArray.length(); k++) {
                                            String areaName = areaJsonArray.getString(k);
                                            areaNames[k] = areaName;
                                        }

                                        //设置areaSpinner的Adapter
                                        areaAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, areaNames);
                                        areaSpinner.setAdapter(areaAdapter);
                                    } catch (
                                            JSONException e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> parent) {

                                }
                            });
                        }
                    } catch (
                            JSONException e) {
                        e.printStackTrace();
                    }
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        } catch (
                IOException e) {
            e.printStackTrace();
        } catch (
                JSONException e) {
            e.printStackTrace();
        }

    }

    //绑定组件
    private void bindView() {
        provinceSpinner = findViewById(R.id.spin_province);
        citySpinner = findViewById(R.id.spin_city);
        areaSpinner = findViewById(R.id.spin_area);
    }
}
